//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.szboanda.platform.v3.workflow.definition;

import com.szboanda.platform.v3.workflow.exception.WorkflowException;
import com.szboanda.platform.v3.workflow.jdbc.JDBCUsersCache;

import java.util.*;

public class WorkflowStepInfo {
    private WorkflowStep step;
    private IWorkflowStepDefinition stepDef;
    private IWorkflow workflow;
    private IWorkflowDefinition workflowDef;
    private List<WorkflowOpinionType> opinionTypes;

    public WorkflowStepInfo() {
    }

    public String getStepId() {
        return this.getStep().getStepId();
    }

    public String getStepDefId() {
        return this.getStepDef().getStepId();
    }

    public String getWorkflowId() {
        return this.getWorkflow().getWorkflowInfo().getWorkflowId();
    }

    public String getWorkflowDefId() {
        return this.getWorkflowDef().getFlowType();
    }

    public String getBusinessId() {
        return this.getWorkflow().getWorkflowInfo().getBusinessId();
    }

    public void setOpinionTypes(List<WorkflowOpinionType> opinionTypes) {
        this.opinionTypes = opinionTypes;
    }

    public List<WorkflowOpinionType> getOpinionTypes() {
        return this.opinionTypes;
    }

    public String getBusinessType() {
        return this.getWorkflowDef().getBusinessType();
    }

    public WorkflowState getWorkflowState() {
        return this.getWorkflow().getWorkflowInfo().getWorkflowState();
    }

    public String getProcesser() {
        return this.getStep().getProcesser();
    }

    public ProcessType getProcessType() {
        return this.getStep().getProcessType();
    }

    public boolean isCurrentStep() {
        return this.getStep() != null && this.getStep().isCurrentStep();
    }

    public boolean isCanSendback() {
        return this.isCurrentStep() && this.getStepDef().canGoBack() && this.getStep().getProcessType().isMaster() && !this.workflowDef.canUserDefine() && !this.isCountersignStep() && !this.isJointProcessStep();
    }

    public boolean isCanAutoTransition() {
        return !this.getWorkflowDef().canUserDefine() && this.isCurrentStep() && this.getStepDef().isAutoTransable() && this.getStep().getProcessType().isMaster() && !this.getStepDef().mustFinish() && !this.isCountersignStep() && !this.isJointProcessStep();
    }

    public boolean isCanTransition() {
        return this.isCurrentStep() && this.getStep().getProcessType().isMaster() && !this.isCountersignStep() && !this.isJointProcessStep() && (!this.getStepDef().mustFinish() || this.getWorkflowDef().canUserDefine());
    }

    public boolean isProcessHelper() {
        return this.isCurrentStep() && this.getStep().getProcessType() == ProcessType.HELPER && !this.isCountersignStep() && !this.isJointProcessStep();
    }

    public boolean isProcessReader() {
        return this.isCurrentStep() && this.getStep().getProcessType() == ProcessType.READER;
    }

    public boolean isCanFinishUserDef() {
        return this.isCurrentStep() && this.getStepDef().canSingleFinish() && !this.isCountersignStep() && !this.isJointProcessStep() && !this.isProcessReader() || this.isCanJointProcessFinish() || this.isCanCountersignFinish();
    }

    public boolean isCanFinish() {
        return this.isCurrentStep() && this.getStepDef().canFinish() && !this.isCountersignStep() && !this.isProcessReader() && !this.isProcessHelper() && !this.isJointProcessStep();
    }

    public boolean isCanSignature() {
        return this.isCurrentStep() && this.getStepDef().canSignature();
    }

    public boolean isCanSplit() {
        return this.isCurrentStep() && this.getStepDef().canSplit();
    }

    public boolean isCanUpload() {
        return this.getStep().getProcessType() != ProcessType.READER;
    }

    public List<IWorkflowAction> getActions() {
        List<IWorkflowAction> actions = new ArrayList();
        List<IWorkflowAction> allActions = this.getStepDef().getActions();
        if (allActions != null) {
            Iterator var3 = allActions.iterator();

            while(var3.hasNext()) {
                IWorkflowAction action = (IWorkflowAction)var3.next();
                if (action.isValid()) {
                    actions.add(action);
                }
            }
        }

        Collections.sort(actions, new Comparator<IWorkflowAction>() {
            public int compare(IWorkflowAction a, IWorkflowAction b) {
                int a1 = a.getPxh();
                int a2 = b.getPxh();
                return a1 - a2;
            }
        });
        return actions;
    }

    public List<IWorkflowAction> getReadonlyActions() {
        List<IWorkflowAction> actions = new ArrayList();
        List<IWorkflowAction> allActions = this.getStepDef().getActions();
        if (allActions != null) {
            Iterator var3 = allActions.iterator();

            while(var3.hasNext()) {
                IWorkflowAction action = (IWorkflowAction)var3.next();
                if (action.isValid() && action.isReadonly()) {
                    actions.add(action);
                }
            }
        }

        return actions;
    }

    public boolean isHasParents() {
        return this.getStep().hasParents();
    }

    public List<WorkflowStep> getParents() {
        return this.getStep().getParents();
    }

    public boolean isNoNextStep() {
        return this.getStepDef().noNextStep();
    }

    public WorkflowStepOptDefinition getStepOpt() {
        return this.getStepDef().getWorkflowStepOpt();
    }

    public WorkflowStep getStep() {
        return this.step;
    }

    public void setStep(WorkflowStep step) {
        this.step = step;
    }

    public IWorkflowStepDefinition getStepDef() {
        return this.stepDef;
    }

    public void setStepDef(IWorkflowStepDefinition stepDef) {
        this.stepDef = stepDef;
    }

    public IWorkflow getWorkflow() {
        return this.workflow;
    }

    public void setWorkflow(IWorkflow workflow) {
        this.workflow = workflow;
    }

    public IWorkflowDefinition getWorkflowDef() {
        return this.workflowDef;
    }

    public void setWorkflowDef(IWorkflowDefinition workflowDef) {
        this.workflowDef = workflowDef;
    }

    public List<WorkflowStep> getAllParents() {
        return this.step.getAllParents();
    }

    public boolean isCanCountersign() {
        if (this.isCurrentStep() && this.getProcessType() == ProcessType.SINGLE_MASTER) {
            boolean b = false;
            Iterator var2;
            if (this.stepDef.getChildrens() != null && this.stepDef.getChildrens().size() > 0) {
                var2 = this.stepDef.getChildrens().iterator();

                while(var2.hasNext()) {
                    IWorkflowStepDefinition stepDef = (IWorkflowStepDefinition)var2.next();
                    if (stepDef.isCountersignStep()) {
                        b = true;
                        break;
                    }
                }
            }

            if (b) {
                var2 = this.getWorkflow().getWorkflowSteps().iterator();
                while(var2.hasNext()) {
                    WorkflowStep step1 = (WorkflowStep)var2.next();
                    String first1 = step1.getCountersignStepTrace().getFirst();
                    if(first1 == null){
                        first1="";
                    }
                    if (step1.getProcessType() != ProcessType.READER && !this.step.getStepId().equals(step1.getStepId())  && first1.equals(this.getStepId()) && step1.isCurrentStep()) {
                        b = false;
                        break;
                    }
                }
            }

            return b;
        } else {
            return false;
        }
    }

    public boolean isCanEndorse() {
        if (this.isCurrentStep() && this.stepDef.isCountersignStep() && this.getProcessType() != ProcessType.READER) {
            WorkflowStackTrace countersignStepTrace = this.step.getCountersignStepTrace();
            CountersignInfo countersignInfo = CountersignCache.getInstance().getCountersignInfo(countersignStepTrace.getFirst());
            if (countersignInfo != null) {
                List<String> processors = countersignInfo.getProcessors();
                CountersignType type = countersignInfo.getType();
                return CountersignType.SERIAL != type || processors.size() > countersignStepTrace.size();
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    public boolean isCanFeedback() {
        if (this.isCurrentStep() && this.stepDef.isCountersignStep() && this.getProcessType() != ProcessType.READER) {
            WorkflowStackTrace countersignStepTrace = this.step.getCountersignStepTrace();
            CountersignInfo countersignInfo = CountersignCache.getInstance().getCountersignInfo(countersignStepTrace.getFirst());
            if (countersignInfo != null) {
                List<String> processors = countersignInfo.getProcessors();
                CountersignType type = countersignInfo.getType();
                return CountersignType.SERIAL != type || processors.size() == countersignStepTrace.size();
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    public String getEndorseProcessor() {
        if (!this.stepDef.isCountersignStep()) {
            throw new WorkflowException("不能在不是会签的步骤上调用获取加签处理人操作!");
        } else {
            WorkflowStackTrace countersignStepTrace = this.step.getCountersignStepTrace();
            CountersignInfo countersignInfo = CountersignCache.getInstance().getCountersignInfo(countersignStepTrace.getFirst());
            if (countersignInfo.getType() != CountersignType.SERIAL) {
                throw new WorkflowException("不是串行会签!");
            } else {
                List<String> processors = countersignInfo.getProcessors();
                int size = countersignStepTrace.size();
                if (processors.size() < size + 1) {
                    throw new WorkflowException("找不到加签处理人!");
                } else {
                    return (String)processors.get(size);
                }
            }
        }
    }

    public boolean isCountersignStep() {
        return this.stepDef.isCountersignStep();
    }

    public boolean isJointProcessStep() {
        return this.stepDef.isJointProcessStep();
    }

    public CountersignType getCountersignType() {
        if (!this.stepDef.isCountersignStep()) {
            return CountersignType.NONE;
        } else {
            WorkflowStackTrace countersignStepTrace = this.step.getCountersignStepTrace();
            CountersignInfo countersignInfo = CountersignCache.getInstance().getCountersignInfo(countersignStepTrace.getFirst());
            CountersignType type = CountersignType.PARALLEL;

            try {
                type = countersignInfo.getType();
            } catch (Exception var5) {
                ;
            }

            return type;
        }
    }

    public boolean isSerialCountersign() {
        return this.getCountersignType() == CountersignType.SERIAL;
    }

    public boolean isUserDefined() {
        return this.getWorkflowDef().canUserDefine();
    }

    public boolean isCanTransitionMaster() {
        List stepDefs;
        if (this.getProcessType() == ProcessType.MULTI_MASTER) {
            if (this.getStepDef().getProcDefinition().getMultiType() == MultiProcessType.SINGLE_PROCESS) {
                return true;
            }

            stepDefs = this.getStepDef().getChildrens();
            if (stepDefs != null && stepDefs.size() == 1) {
                IWorkflowStepDefinition stepDef = (IWorkflowStepDefinition)stepDefs.get(0);
                if (stepDef.isMargeMulti()) {
                    WorkflowJoinType joinType = stepDef.getJoinType();
                    List<WorkflowStep> masterSteps = this.findOtherMultiMasterSteps(this.getStep(), joinType);
                    if (masterSteps != null && masterSteps.size() > 0) {
                        WorkflowStep joinedStep = this.multiMasterTransed(stepDef, this.getStep(), masterSteps);
                        if (WorkflowJoinType.FirstStepJoin == joinType) {
                            if (joinedStep != null) {
                                return false;
                            }
                        } else if (WorkflowJoinType.LastStepJoin == joinType && joinedStep == null) {
                            boolean allMultiMasterWaited = this.allMultiMasterWaited(this.getStep(), masterSteps);
                            if (!allMultiMasterWaited) {
                                return false;
                            }
                        }
                    }
                }
            }
        }

        if (this.stepDef.noNextStep()) {
            return false;
        } else {
            stepDefs = this.stepDef.getChildrens();
            boolean judge = true;
            Iterator var8 = stepDefs.iterator();

            while(var8.hasNext()) {
                IWorkflowStepDefinition nextStepDef = (IWorkflowStepDefinition)var8.next();
                if (!nextStepDef.isCountersignStep() && !nextStepDef.isJointProcessStep() && !nextStepDef.isLastStep()) {
                    judge = false;
                    break;
                }
            }

            return !judge;
        }
    }

    private List<WorkflowStep> findOtherMultiMasterSteps(WorkflowStep current, WorkflowJoinType joinType) {
        List<WorkflowStep> steps2 = new ArrayList();
        String stepDefId = current.getStepDefId();
        Iterator var5 = this.getWorkflow().getWorkflowSteps().iterator();

        while(var5.hasNext()) {
            WorkflowStep step = (WorkflowStep)var5.next();
            if (WorkflowJoinType.FirstStepJoin != joinType) {
                if (step.getProcessType().isMaster() && step.isCurrentStep() && stepDefId.equals(step.getStepDefId()) && !current.getStepId().equals(step.getStepId()) && current.getStackTrace().isBrotherOrChildNode(step.getStackTrace())) {
                    steps2.add(step);
                }
            } else if (step.getProcessType().isMaster() && stepDefId.equals(step.getStepDefId()) && !current.getStepId().equals(step.getStepId()) && current.getStackTrace().isBrotherOrChildNode(step.getStackTrace())) {
                steps2.add(step);
            }
        }

        return steps2;
    }

    private WorkflowStep multiMasterTransed(IWorkflowStepDefinition stepDef, WorkflowStep current, List<WorkflowStep> masterSteps) {
        Iterator var4 = masterSteps.iterator();

        while(true) {
            WorkflowStep step;
            do {
                do {
                    do {
                        if (!var4.hasNext()) {
                            return null;
                        }

                        step = (WorkflowStep)var4.next();
                    } while(step.getState() != WorkflowStepState.Finished);
                } while(!current.getStackTrace().equals(step.getStackTrace()));
            } while(!step.hasChildrens());

            Iterator var6 = step.getChildrens().iterator();

            while(var6.hasNext()) {
                WorkflowStep step3 = (WorkflowStep)var6.next();
                if (stepDef.getStepId().equals(step3.getStepDefId()) && step3.getProcessType().isMaster()) {
                    return step3;
                }
            }
        }
    }

    private boolean allMultiMasterWaited(WorkflowStep current, List<WorkflowStep> masterSteps) {
        boolean all = true;
        Iterator var4 = masterSteps.iterator();

        while(true) {
            while(true) {
                WorkflowStep step;
                do {
                    if (!var4.hasNext()) {
                        return all;
                    }

                    step = (WorkflowStep)var4.next();
                } while(current.getStepId().equals(step.getStepId()));

                boolean finished;
                if (current.getStackTrace().equals(step.getStackTrace()) && current.getStepDefId().equals(step.getStepDefId())) {
                    finished = step.getState() == WorkflowStepState.Wait;
                    all = all && finished;
                } else {
                    finished = step.getState() == WorkflowStepState.Finished;
                    all = all && finished;
                }
            }
        }
    }

    public boolean isCanJointProcessFeedback() {
        if (!this.isJointProcessStep()) {
            return false;
        } else {
            WorkflowStackTrace trace = this.step.getJointProcessStepTrace();
            JointProcessInfo info = JointProcessCache.getInstance().getJointProcessInfo(trace.getFirst());
            if (info != null && !info.isCollect() && trace.size() == 1) {
                return false;
            } else {
                return this.isCurrentStep() && this.isJointProcessStep() && this.getProcessType() != ProcessType.READER && this.getStep().getState() != WorkflowStepState.AlreadyFeedback;
            }
        }
    }

    public boolean isCanJointProcessTransition() {
        return this.isCurrentStep() && this.isJointProcessStep() && this.getProcessType() != ProcessType.READER && this.getStep().getState() != WorkflowStepState.AlreadyFeedback;
    }

    public boolean isCanJointProcessFinish() {
        if (this.isJointProcessStep() && this.isCurrentStep() && this.step.getProcessType().isMaster()) {
            WorkflowStackTrace trace = this.step.getJointProcessStepTrace();
            JointProcessInfo info = JointProcessCache.getInstance().getJointProcessInfo(trace.getFirst());
            if (null != info && !info.isCollect()) {
                return true;
            }

            if (this.getStep().getJointProcessStepTrace() != null && this.getStep().getJointProcessStepTrace().size() > 1) {
                return true;
            }

            if (this.getStep().getJointProcessStepTrace() != null && this.getStep().getJointProcessStepTrace().size() == 1 && this.getStep().getState() == WorkflowStepState.AlreadyFeedback) {
                return true;
            }
        }

        return false;
    }

    public List<IUser> getJointProcessFeedbackUsers() {
        if (!this.isJointProcessStep()) {
            throw new WorkflowException("不是会办步骤,不能获取返回人!");
        } else {
            List<IUser> users = new ArrayList();
            Set<String> userIds = new HashSet();
            WorkflowStackTrace jointProcessStackTrace = this.getStep().getJointProcessStepTrace().clone();
            if (jointProcessStackTrace.size() == 1) {
                JointProcessInfo info = JointProcessCache.getInstance().getJointProcessInfo(jointProcessStackTrace.getFirst());
                IUser user = JDBCUsersCache.getInstance().getUser(info.getOpinionProcessor());
                if (user != null) {
                    users.add(user);
                }
            } else {
                List<WorkflowStep> parents = this.getAllParents();
                Iterator var9 = parents.iterator();

                while(var9.hasNext()) {
                    WorkflowStep parent = (WorkflowStep)var9.next();
                    if (parent.getDefinition().isJointProcessStep() && parent.getJointProcessStepTrace() != null && parent.getJointProcessStepTrace().isSameSplit(jointProcessStackTrace) && !userIds.contains(parent.getProcesser())) {
                        IUser user = JDBCUsersCache.getInstance().getUser(parent.getProcesser());
                        if (user != null) {
                            users.add(user);
                        }
                    }
                }
            }

            return users;
        }
    }

    public boolean canTurnDeal() {
        return this.stepDef.canTurnDeal() && this.getStepOpt().isShowJb() && this.getProcessType() != ProcessType.READER;
    }

    public boolean isCanJointProcess() {
        if (!this.isCurrentStep()) {
            return false;
        } else if (this.getProcessType() != ProcessType.SINGLE_MASTER && this.getProcessType() != ProcessType.MULTI_MASTER) {
            return false;
        } else {
            boolean b = false;
            if (this.stepDef.getChildrens() != null && this.stepDef.getChildrens().size() > 0) {
                Iterator var2 = this.stepDef.getChildrens().iterator();

                while(var2.hasNext()) {
                    IWorkflowStepDefinition stepDef = (IWorkflowStepDefinition)var2.next();
                    if (stepDef.isJointProcessStep()) {
                        b = true;
                        break;
                    }
                }
            }

            return b;
        }
    }

    public boolean isCanCountersignFinish() {
        if (this.isCurrentStep() && this.stepDef.isCountersignStep() && this.getProcessType() != ProcessType.READER) {
            WorkflowStackTrace countersignStepTrace = this.step.getCountersignStepTrace();
            CountersignInfo countersignInfo = CountersignCache.getInstance().getCountersignInfo(countersignStepTrace.getFirst());
            return countersignInfo == null;
        } else {
            return false;
        }
    }
}
